import * as fgui from "fairygui-cc";
import { Component, math, screen, view } from "cc";
import IUIWindow from "./IUIWindow";
import Instance from "../../../Core/Utils/Instance";
import ArrayUtil from "../../../Core/Utils/ArrayUtil";
import EventMgr from "../../../Core/Event/EventMgr";
import WindowManagerEvent from "./WindowManagerEvent";
import ModuleWindow from "./ModuleWindow";

export default class WindowManager {
    private _root: fgui.GComponent;
    private _windows: Array<IUIWindow>;
    private _modalWindows: Array<IUIWindow>;
    private _touchWindows: Array<IUIWindow>;
    private _modalLayer: fgui.GGraph;
    private _touchLayer: fgui.GLoader;
    public static get ins(): WindowManager {
        return Instance.get(WindowManager);
    }
    public init(): void {
        this._windows = new Array<IUIWindow>();
        this._modalWindows = new Array<IUIWindow>();
        this._touchWindows = new Array<IUIWindow>();
        this._modalLayer = new fgui.GGraph();
        this._touchLayer = new fgui.GLoader();
        this._touchLayer.fill = fgui.LoaderFillType.ScaleFree;
        screen.on("window-resize", this.onResize, this);
        this.onResize();
        // GGraph先draw出来，在添加点击事件才生效
        this._touchLayer.onClick(this.onTouchLayerClick, this);
    }
    /**
     * 显示窗体
     */
    public showWindow(window: IUIWindow): void {
        var view: fgui.GComponent = window.view;
        if (view == null) {
            return;
        }
        // 显示模态
        if (window.modal == true) {
            if (this._modalLayer.parent != this._root) {
                this.showModal();
            }
            this._root.addChild(this._modalLayer);
            ArrayUtil.removeItems(this._modalWindows, window);
            this._modalWindows.push(window);
        }
        // 显示触摸
        if (window.touchClose == true) {
            if (this._touchLayer.parent != this._root) {
                this.showTouch();
            }
            this._root.addChild(this._touchLayer);
            ArrayUtil.removeItems(this._touchWindows, window);
            this._touchWindows.push(window);
            if (window.windowData) {
                this._touchLayer.name = window.windowData.WindowName;
            }
        }
        // 显示窗体
        if (view.parent == null && window.needShowAction == true) {
            window.playShowAnimation();
        }
        this._root.addChild(view);
        view.makeFullScreen();
        window.isShow = true;
        ArrayUtil.removeItems(this._windows, window);
        this._windows.push(window);
        EventMgr.emit(WindowManagerEvent.SHOW_WINDOW, window);
    }
    /**
     * 关闭窗体
     */
    public closeWindow(window: IUIWindow): void {
        if (window.isShow == false) {
            return;
        }
        const view: fgui.GComponent = window.view;
        // 移除窗体
        this._root.removeChild(view);
        window.view.dispose();
        ArrayUtil.removeItems(this._windows, window);
        window.isShow = false;
        // 处理模态层
        if (window.modal == true) {
            // 是否为最上层模态窗体
            const isTail: boolean = this._modalWindows[this._modalWindows.length - 1] == window;
            ArrayUtil.removeItems(this._modalWindows, window);
            if (isTail == true) {
                // 获取上一个模态窗体
                const w: IUIWindow = this._modalWindows[this._modalWindows.length - 1];
                if (w == null) {
                    this._root.removeChild(this._modalLayer);
                } else {
                    // 重设模态层索引
                    let windowIndex: number = this._root.getChildIndex(w.view);
                    const index: number = this._root.getChildIndex(this._modalLayer);
                    if (index > windowIndex) {
                        this._root.setChildIndex(this._modalLayer, windowIndex);
                    }
                    if (w.touchClose) {
                        windowIndex = this._root.getChildIndex(w.view);
                        if (window.touchClose == false) {
                            windowIndex = windowIndex - 1;
                        }
                        this._root.setChildIndex(this._touchLayer, windowIndex);
                    }
                }
            }
        }
        // 处理触摸层
        if (window.touchClose == true) {
            // 是否为最上层触摸窗体
            const isTail: boolean = this._touchWindows[this._touchWindows.length - 1] == window;
            ArrayUtil.removeItems(this._touchWindows, window);
            if (isTail == true) {
                // 获取上一个触摸窗体
                const w: IUIWindow = this._touchWindows[this._touchWindows.length - 1];
                if (w == null) {
                    this._root.removeChild(this._touchLayer);
                } else {
                    // 重设触摸层索引
                    let windowIndex: number = this._root.getChildIndex(w.view);
                    const index: number = this._root.getChildIndex(this._touchLayer);
                    if (index > windowIndex) {
                        this._root.setChildIndex(this._touchLayer, windowIndex);
                    }
                    if (w.windowData) {
                        this._touchLayer.name = w.windowData.WindowName;
                    }
                }
            }
        }
        ModuleWindow.ins.removeWindow(window);
        EventMgr.emit(WindowManagerEvent.CLOSE_WINDOW, window);
    }
    /**
         * 设置window到最上层
         */
    public bringToFront(window: IUIWindow): void {
        if (window.isShow == false) {
            return;
        }
        this.showWindow(window);
    }
    /**
     * 设置窗体坐标
     */
    public positionWindow(window: IUIWindow, x?: number, y?: number): void {
        const view: fgui.GComponent = window.view;
        if (view == null) {
            return;
        }
        // 设置窗体位置
        if (!isNaN(x) && !isNaN(y)) {
            window.isShowCenter = false;
            view.x = x;
            view.y = y;
        } else {
            window.isShowCenter = true;
            view.center();
        }
    }
    /**
     * 显示触摸
     */
    private showTouch(): void {
        // 在此可显示触摸动画
        this._touchLayer.url = fgui.UIPackage.getItemURL(`Common`, `mengban`);
    }
    /**
     * 显示模态
     */
    private showModal(): void {
        // 在此可显示模态动画
    }
    public initRoot(root: fgui.GComponent): void {
        this._root = root;
    }
    private onResize(): void {
        const screenWidth: number = view.getVisibleSize().width;
        const screenHeight: number = view.getVisibleSize().height;
        this._modalLayer.setSize(screenWidth, screenHeight);
        this._modalLayer.drawRect(0, new math.Color(0, 0, 0, 0.8), new math.Color(0, 0, 0, 0.8));
        this._touchLayer.setSize(screenWidth, screenHeight);
        this._touchLayer.url = fgui.UIPackage.getItemURL(`Common`, `mengban`);
        for (const window of this._windows) {
            if (window.isShowCenter) {
                window.view.center();
            }
        }
    }
    private onTouchLayerClick(): void {
        console.log(`点击关闭层`)
        if (this._touchWindows.length > 0) {
            const window: IUIWindow = this._touchWindows[this._touchWindows.length - 1];
            if (!window.touchClose) {
                return;
            }
            window.close();
        }
    }
    /**
    * 绑定自定义变量
    * @param contentPane
    * @param thisObject
    */
    public setSkinPart(contentPane: fgui.GComponent, thisObject: any): void {
        const cnt = contentPane._children.length;
        let name = "";
        for (let i = 0; i < cnt; ++i) {
            name = contentPane._children[i].name;
            thisObject[name] = contentPane._children[i] as any;
        }
        const _length: number = contentPane._controllers.length;
        for (let i = 0; i < _length; i++) {
            name = contentPane._controllers[i].name;
            if (name.indexOf('ctrl_') > -1) {
                thisObject[name] = contentPane._controllers[i] as fgui.Controller;

            }
        }
    }
}